<?php

namespace App\Models;


use function foo\func;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Comment extends Model
{

    use SoftDeletes;


    protected $table = 'postscomment';

//    public $timestamps = false;

    protected $guarded = [];


    public  function subReplys(){
        return $this->hasMany(Comment::class,'isreply','id');
    }


    public function comment(){

        return $this->belongsTo(Post::class,'posts_id','id');
    }

    public function user(){

        return $this->belongsTo(User::class,'comment_uid','id');

    }


    // 评论所在的帖子
    public function post(){
        return $this->belongsTo(Post::class,'posts_id','id');
    }

    /**
     *
     * 评论删除的时候同时删除子评论
     *
     *
     * @return bool|null
     */
    public function delete()
    {
        $this->subReplys->each->delete();

        return parent::delete();
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($comment){

            $comment->comment_time = time();

        });

    }


}
